package com.sgb.knowledgeshare.view.fragment;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareMianBinding;
import com.sgb.knowledgeshare.view.adapter.ShareCommPagerAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareMianModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.ArrayList;

/**
 * 分享-首页
 */
public class ShareMianFragment extends AppLazyFragment {
    ShareMianModel viewmode;
    private FragmentShareMianBinding binding;
    private ArrayList<Fragment> fragments;
    private int curPage;
    private String[] titles = {"关注", "推荐"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_mian, container, false);
        viewmode = new ViewModelProvider(this).get(ShareMianModel.class);
        viewmode.setActivity(ShareMianFragment.this);
        binding.setViewModel(viewmode);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        binding.backView.setOnClickListener(this);
        binding.viewpager.setNoScroll(true);
        fragments = new ArrayList<>();
        fragments.add(new AttentionFragment());
        fragments.add(new RecommendFragment());
        ShareCommPagerAdapter pagerAdapter = new ShareCommPagerAdapter(getChildFragmentManager(), fragments, titles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length - 1);
        binding.tabTitle.setupWithViewPager(binding.viewpager);
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position>0){
                    binding.tabTitle.setTabTextColors(getResources().getColor(R.color.color_CCCCCC),getResources().getColor(R.color.white));
                    View decor = getActivity().getWindow().getDecorView();
                    decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                    binding.backView.setImageResource(R.mipmap.back);
                }
                curPage = position;
                SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.SHARE_HOME_POSITION, curPage + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        binding.viewpager.setCurrentItem(1);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back_view) {
            getActivity().finish();
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (fragments != null && fragments.size() > 0) {
            Fragment fragment = fragments.get(curPage);
            if (fragment instanceof AttentionFragment) {
                ((AttentionFragment) fragment).onVisible();
            }
            if (fragment instanceof RecommendFragment) {
                ((RecommendFragment) fragment).onVisible();
            }
        }
    }

    @Override
    public void onInvisible() {
        super.onInvisible();
        if (fragments != null && fragments.size() > 0) {
            Fragment fragment = fragments.get(curPage);
            if (curPage==0&&fragment instanceof AttentionFragment) {
                ((AttentionFragment) fragment).onInvisible();
            }
            if (curPage==1&&fragment instanceof RecommendFragment) {
                ((RecommendFragment) fragment).onInvisible();
            }
            /*for (Fragment fragment : fragments) {
                if (fragment != null) {
                    if (fragment instanceof AttentionFragment) {
                        ((AttentionFragment) fragment).onInvisible();
                    }
                    if (fragment instanceof RecommendFragment) {
                        ((RecommendFragment) fragment).onInvisible();
                    }
                }
            }*/
        }
    }

    public void updateBottomViews(float slideOffset, String userno) {
        binding.topLayout.setVisibility(slideOffset == 1 ? View.GONE : View.VISIBLE);
        if (getParentFragment() != null) {
            ((ShareHomeFragment) getParentFragment()).updateBottomViews(slideOffset,userno);
        }
    }

    public void setTitleViews() {
        binding.tabTitle.setTabTextColors(getResources().getColor(R.color.color_CCCCCC),getResources().getColor(R.color.color_5792FD));
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.backView.setImageResource(R.mipmap.btn_back3);
    }

    public void updataUserInfo(VideoBean recordsBean, int total) {
        if (getParentFragment() != null) {
            ((ShareHomeFragment) getParentFragment()).updataUserInfos(recordsBean,total);
        }
    }
}
